import 'package:flutter/material.dart';

//广播
import '../../services/EventBus.dart';

// 购物车每一行的增加或者减少按钮
class CartNum extends StatefulWidget{
  final int count;
  CartNum(this.count);

  @override
  State<StatefulWidget> createState() => CartNumState(count);
}

class CartNumState extends State{
  int count;
  CartNumState(this.count);

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        InkWell(
          // 减少按钮
          child: Container(
            alignment: Alignment.center,
            width: 30,
            height: 30,
            decoration: BoxDecoration(
                border: Border.all(
                    color: Colors.black12
                )
            ),
            child: Text('-'),
          ),
          onTap: (){
            if(this.count > 1) {
              this.setState(() {
                count -= 1;

                // 广播
                eventBus.fire(new ProductContentFirstCountEvent(-1));
              });
            }
          },
        ),

        // 显示数量
        InkWell(
          child: Container(
            alignment: Alignment.center,
            width: 30,
            height: 30,
            decoration: BoxDecoration(
                border: Border.all(
                    color: Colors.black12
                )
            ),
            child: Text('$count'),
          ),
          onTap: (){},
        ),

        // 增加按钮
        InkWell(
          child: Container(
            alignment: Alignment.center,
            width: 30,
            height: 30,
            decoration: BoxDecoration(
                border: Border.all(
                    color: Colors.black12
                )
            ),
            child: Text('+'),
          ),
          onTap: (){
            this.setState(() {
              this.count += 1;

              // 广播
              eventBus.fire(new ProductContentFirstCountEvent(1));
            });
          },
        ),
      ],
    );
  }
}
